using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using BridgeUI;

namespace BrSample
{
    /// <summary>
    /// 这是游戏的主类型
    /// </summary>
    public class UserApp : BrApp
    {
        public UserApp()
        {
            Content.RootDirectory = "Content";

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);

            // Extend battery life under lock.
            InactiveSleepTime = TimeSpan.FromSeconds(1);
        }

        private Model planeModel;
        private ModelControl Plane;
        private GameTimer timer; // 注意，这里明显会出现BUG。。。使用了多线程，之后应该添加对 Timer 的原生支持。

        protected override void Prepare()
        {

        }

        protected override void LoadContent()
        {
            planeModel = Content.Load<Model>("Models\\p1_wedge");
            base.LoadContent();
        }

        private bool rotate = false;
        protected override void LoadComponent()
        {
            World.Observer.ViewableDistance = new Range(10, 10000); // 默认的 [1, 1000] 对于咱们的飞机太小
            World.Controls.Add(Plane = new ModelControl(planeModel) {Postion = Vector3.UnitZ*5000});
            Plane.FocusEnter += Plane_FocusEnter;
            Plane.FocusLeave += (sender, args) => rotate = false;
            timer = new GameTimer(){UpdateInterval = TimeSpan.FromMilliseconds(30)};
            timer.Update += (sender, e) => tick(null);
        }

        void Plane_FocusEnter(object sender, EventArgs e)
        {
            rotate = true;
            Console.WriteLine("You click me!");
        }


        protected override void ComponentLoaded()
        {
            timer.Start();
        }


        void tick(object state)
        {
            if (rotate)
                Plane.Orientation = Vector3.Transform(Plane.Orientation, Matrix.CreateRotationY(MathHelper.Pi / 30));
        }
    }
}
